<?php

namespace App\Log\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use App\Log\Service\LogViewerService;
class HomeController extends Controller
{
    
    public function __construct(LogViewerService $logViewerService)
    {
        parent::__construct($logViewerService);
        
    }
    /**
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function home(Request $request)
    {
        if ($request->has('file')) {
            $this->service->setLogPath($request->input('file'));
            $viewName = 'detail';
        } else {
            $viewName = 'home';
        }
        return view($viewName, [
            'service'  => $this->service,
            'keywords' => $request->input('keywords'),
        ]);
    }

    /**
     * 文件下载
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
     */
    public function download(Request $request)
    {
        ini_set('memory_limit','400M');
        $this->service->setLogPath($request->input('file'));
        ob_clean();//清理缓存
        return response()->download($this->service->getLogPath())->deleteFileAfterSend(true);
    }
    //打印文件
    public function ddcontent(Request $request)
    {
        $this->service->setLogPath($request->input('file'));
        $this->service->ddgetLogContents();
    }

    /**
     * 删除文件
     * @param Request $request
     * @return array
     */
    public function delete7(Request $request)
    {
        $this->service->setLogPath($request->input('file'));
        if (File::delete($this->service->getLogPath())) {
            //die("555");
            return ['status' => 'success', 'message' => '删除成功', 'redirect' => route('log-viewer-home')];
        }
        return ['status' => 'fail', 'message' =>'删除失败'];
    }
    public function delete(Request $request)
    {
        $this->service->setLogPath($request->input('file'));
        File::delete($this->service->getLogPath());
        $url=route('log-viewer-home');
        echo '<script>window.location.href="'.$url.'";</script>';
        exit();

        
        
    }

}
